﻿using AutoUpdaterDotNET;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;

namespace GuiZhou.ImageArchive.Update
{
    public class UpdateTool
    {
        public static void CheckUpdate()
        {
            //处理非xml升级配置文件（json等），请除掉注释
            //AutoUpdater.ParseUpdateInfoEvent += AutoUpdaterOnParseUpdateInfoEvent;

            //允许非管理员身份升级，请除掉注释
            //AutoUpdater.RunUpdateAsAdmin = false;


            //切换语言，请除掉注释
            Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("zh");


            //如果不直接下载，而是打开下载页面，请除掉注释
            //AutoUpdater.OpenDownloadPage = true;

            //Don't want user to select remind later time in AutoUpdater notification window then uncomment 3 lines below so default remind later time will be set to 2 days.
            //如果自定义下次提醒日期，请除掉注释，否则，下次提醒时间默认为2天
            //AutoUpdater.LetUserSelectRemindLater = false;
            //AutoUpdater.RemindLaterTimeSpan = RemindLaterFormat.Minutes;
            //AutoUpdater.RemindLaterAt = 1;

            //Don't want to show Skip button then uncomment below line.
            //如果不允许跳过新版本，请除掉注释
            //AutoUpdater.ShowSkipButton = false;

            //Don't want to show Remind Later button then uncomment below line.
            //如果不允许稍后提醒，请除掉注释
            //AutoUpdater.ShowRemindLaterButton = false;

            //Want to show custom application title then uncomment below line.
            //设置自定义标题，请除掉注释
            AutoUpdater.AppTitle = "系统更新";

            //Want to show errors then uncomment below line.
            //如果开启升级错误报告，请除掉注释
            //AutoUpdater.ReportErrors = true;

            //Want to handle how your application will exit when application finished downloading then uncomment below line.
            //处理程序退出时的动作
            //AutoUpdater.ApplicationExitEvent += AutoUpdater_ApplicationExitEvent;

            //Want to handle update logic yourself then uncomment below line.
            //自定义升级方式（FTP等）,请除掉注释
            //AutoUpdater.CheckForUpdateEvent += AutoUpdaterOnCheckForUpdateEvent;

            //Want to use XML and Update file served only through Proxy.
            //仅用代理服务器
            //var proxy = new WebProxy("localproxyIP:8080", true) {Credentials = new NetworkCredential("domain\\user", "password")};
            //AutoUpdater.Proxy = proxy;

            //Want to check for updates frequently then uncomment following lines.
            //定时更新，请解开注释
            //System.Timers.Timer timer = new System.Timers.Timer
            //{
            //    Interval = 2 * 60 * 1000,
            //    SynchronizingObject = this
            //};
            //timer.Elapsed += delegate
            //{
            //    AutoUpdater.Start("https://rbsoft.org/updates/AutoUpdaterTest.xml");
            //};
            //timer.Start();

            //是否强制更新
            //AutoUpdater.Mandatory = false; 

            //解压缩程序路径
            // AutoUpdater.ZipExtratorPath =ConfigurationManager.AppSettings["UpdataPathZip"]+ "\\package\\ZipExtrator.exe";
            // AutoUpdater.ZipExtratorPath = AppDomain.CurrentDomain.BaseDirectory + "package\\ZipExtractor.exe";
            //AutoUpdater.ZipExtratorPath= Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "package\\ZipExtractor.exe");
            // AutoUpdater.ZipExtratorPath=Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "package\\", "ZipExtractor.exe");
            string basedire = AppDomain.CurrentDomain.BaseDirectory;
            string dead = basedire + "package\\ZipExtractor.exe";
            AutoUpdater.ZipExtratorPath = dead;
            //读取更新配置文件地址
            string updateConfigURL = ConfigurationManager.AppSettings["UpdateConfigURL"];

            //获取待更新程序的信息
            Assembly updateProgramAss = GetAssembly();

            //开始检查更新
            AutoUpdater.Start(updateConfigURL, false,updateProgramAss);
        }

        public static Assembly GetAssembly()
        {
            string programName = ConfigurationManager.AppSettings["UpdateProgram"];
            string updateProgramFullPath = AppDomain.CurrentDomain.BaseDirectory + programName;
            Assembly programAss = Assembly.LoadFile(updateProgramFullPath);
            return programAss;
        }

    }
}
